package com.tnovoselec.android.travelassistant.activity;

import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.AdapterView;

import com.google.android.maps.GeoPoint;
import com.j256.ormlite.dao.Dao;
import com.tnovoselec.android.common.async.Async;
import com.tnovoselec.android.common.async.listener.Outcome;
import com.tnovoselec.android.common.async.notif.NotifierFactory;
import com.tnovoselec.android.lbs.location.MyLocationService;
import com.tnovoselec.android.travelassistant.Constant;
import com.tnovoselec.android.travelassistant.R;
import com.tnovoselec.android.travelassistant.adapter.AbstractAdapter;
import com.tnovoselec.android.travelassistant.adapter.BenzinskeAdapter;
import com.tnovoselec.android.travelassistant.async.BenzinskeRefresher;
import com.tnovoselec.android.travelassistant.async.BenzinskeSearcher;
import com.tnovoselec.android.travelassistant.dto.BenzinskaDto;
import com.tnovoselec.android.travelassistant.dto.BenzinskeResultHolder;
import com.tnovoselec.android.travelassistant.dto.IGeoPoint;
import com.tnovoselec.android.travelassistant.overlay.BenzinskeAROverlayNavigation;
import com.tnovoselec.android.travelassistant.overlay.BenzinskeOverlay;
import com.tnovoselec.android.travelassistant.overlay.BenzinskeOverlay.BenzinskaSelectedListener;
import com.tnovoselec.android.ui.quickaction.ActionItem;
import com.tnovoselec.android.ui.quickaction.QuickAction;

public class BenzinskeMapActivity extends AbstractMapActivity implements BenzinskaSelectedListener{

	public static String TAG = BankomatiMapActivity.class.getSimpleName();

	
	private static final int INA_ID = 1;
	private static final int CROBENZ_ID = 2;
	private static final int OMV_ID = 3;
	private static final int PETROL_ID = 4;
	private static final int ALL_ID = 5;

	

	
	protected synchronized void search(final GeoPoint geoPoint, final VIEWS view) {
		dialog = ProgressDialog.show(this, "", super.getResources().getText(R.string.please_wait), true);

		Async.doAsync(null, new BenzinskeSearcher(this, poiDao, geoPoint, activeType), new Outcome<Void, Boolean>() {

			@SuppressWarnings("synthetic-access")
			@Override
			protected void succeeded(Void key, Boolean result) {
				dialog.dismiss();

				if (result.booleanValue()) {
					if (view == VIEWS.MAP) {
						Drawable mapPin = getResources().getDrawable(R.drawable.map_pin);
						mapOverlay = new BenzinskeOverlay(BenzinskeMapActivity.this, mapPin, BenzinskeResultHolder.getAllBenzinske(), mapView);
						((BenzinskeOverlay)mapOverlay).setListener(BenzinskeMapActivity.this);
						// benzinskeOverlay.adjustMapViewToPOIs(mapView,
						// MyLocationService.getLastKnownGeoPoint(BenzinskeMapActivity.this));
						mapOverlay.adjustMapViewToPOIs(mapView, null);
						mapView.getOverlays().clear();
						mapView.getOverlays().add(mapOverlay);
						mapView.getOverlays().add(locationOverlay);
					} else if (view == VIEWS.AR) {

						BenzinskeAROverlayNavigation overlay = new BenzinskeAROverlayNavigation(arAugmentedView);
						overlay.setPoiARList(BenzinskeResultHolder.getAllBenzinske());
						arAugmentedView.removeAllOverlays();
						arAugmentedView.addOverlay(overlay);
						arAugmentedView.setLocation(MyLocationService.getLastKnownLocation(BenzinskeMapActivity.this));
					}
				} else {
					// ImageToast.show(HomeActivity.this, R.drawable.icon,
					// R.string.no_result);
				}

			}

			@Override
			protected void failed(Void key, Throwable error) {
				dialog.dismiss();

				Log.e(TAG, "Exception: ", error);
			}
		}, NotifierFactory.newThreadSpecificNotifier(handler));
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		viewFlipper.setDisplayedChild(0);
		BenzinskaDto item = (BenzinskaDto) adapter.getItem(position);
		// mapView.disableOnMoveListener();
		// mapView.animateTo(item.getGeoPoint());
		if (mapOverlay != null) {
			mapOverlay.handleListClick(item);
		}
	}

	

	@Override
	public void onAnimationStart(Animation animation) {
		// NIX

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		if (animation == animHide) {
			searchContainer.setVisibility(View.GONE);
			isSearchActivated = false;
			if (query != null) {
				query.setText("");
			}
		}

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// NIX

	}

	

	@Override
	public void onBenzinskaSelected() {
		hideSearchPopup();

	}

	@Override
	public void onItemClick(QuickAction source, int pos, int actionId) {
		if (source == quickAction){
			if (actionId == INA_ID){
				activeType = Constant.INA_REGEX;
			} else if (actionId == PETROL_ID){
				activeType = Constant.PETROL_REGEX;
			} else if (actionId == OMV_ID){
				activeType = Constant.OMV_REGEX;
			} else if (actionId == CROBENZ_ID){
				activeType = Constant.CROBENZ_REGEX;
			} else if (actionId == ALL_ID){
				activeType = "";
			}
			//search(MyLocationService.getLastKnownGeoPoint(this), activeView);
			//search(mapView.getCurrentMiddlePoint(), activeView);
			super.onItemClick(source, pos, actionId);
		}

	}


	@Override
	public void onMapMoved(GeoPoint tl, GeoPoint br) {
		 Async.doAsync(null, new BenzinskeRefresher(this, poiDao, tl, br , activeType), new Outcome<Void, Boolean>() {

             @SuppressWarnings("synthetic-access")
             @Override
             protected void succeeded(Void key, Boolean result) {
                     if (result.booleanValue()) {
                             handler.removeCallbacks(updateMap);
                             handler.postDelayed(updateMap, 1000L);
                     } else {
                             // NIX
                     }

             }

             @Override
             protected void failed(Void key, Throwable t) {
                     // NIX

             }
     }, NotifierFactory.newThreadSpecificNotifier(handler));
		
	}


	@Override
	public Dao<IGeoPoint, String> getDao() {
		try {
			return this.getHelper().getBenzinskaDao();
		} catch (Exception e) {
			Log.e(TAG, "dao fetch failed");
		}
		return null;
	}


	@Override
	public AbstractAdapter<IGeoPoint> getAdapter() {

		return new BenzinskeAdapter(this);
	}


	@Override
	public void populateQuickAction() {
		ActionItem inaAction = new ActionItem(INA_ID, getString(R.string.ina_title), getResources().getDrawable(R.drawable.ina_icon));
		ActionItem crobenzAction = new ActionItem(CROBENZ_ID, getString(R.string.crobenz_title), getResources().getDrawable(R.drawable.crobenz_icon));
		ActionItem omvAction = new ActionItem(OMV_ID, getString(R.string.omv_title), getResources().getDrawable(R.drawable.omv_icon));
		ActionItem petrolAction = new ActionItem(PETROL_ID, getString(R.string.petrol_title), getResources().getDrawable(R.drawable.petrol_icon));
		ActionItem allAction = new ActionItem(ALL_ID, getString(R.string.all_gas_title), getResources().getDrawable(R.drawable.all));

	
		quickAction.addActionItem(inaAction);
		quickAction.addActionItem(crobenzAction);
		quickAction.addActionItem(omvAction);
		quickAction.addActionItem(petrolAction);

		quickAction.addActionItem(allAction);
		quickAction.setOnActionItemClickListener(this);

		
	}


	@Override
	public Runnable getUpdateMapRunnable() {
		// TODO Auto-generated method stub
		return new Runnable() {

			@SuppressWarnings("synthetic-access")
			@Override
			public void run() {
				Drawable mapPin = getResources().getDrawable(R.drawable.map_pin);
				mapOverlay = new BenzinskeOverlay(BenzinskeMapActivity.this, mapPin, BenzinskeResultHolder.getAllBenzinske(), mapView);
				((BenzinskeOverlay)mapOverlay).setListener(BenzinskeMapActivity.this);
				// bankomatiOverlay.adjustMapViewToPOIs(mapView, null);
				mapView.getOverlays().clear();
				mapView.getOverlays().add(mapOverlay);
				mapView.getOverlays().add(locationOverlay);
				mapView.invalidate();
			}
		};
	}


	@Override
	public void updateList() {
		adapter.setData(BenzinskeResultHolder.getAllBenzinske());
		
	}

}
